﻿@inject INavInvReportClassService NavInvReportClassService


<div align="center">
    <label>Fs Subtotal & Recv. vs Cons. Trend</label>
</div>
<Chart @ref="_lineChart" ChartType="ChartType.Line" OnInitAsync="@OnInit" Height="100%" />
@code {
    private Chart _lineChart = default!;
    private List<ChartDoubleDataDto> sbutotalDataDtos = new();
    private List<ChartDoubleDataDto> recvTrendDataDtos = new();
    private List<ChartDoubleDataDto> consTrendDataDtos = new();

    private async Task<ChartDataSource> OnInit()
    {
        var response = await NavInvReportClassService.GetAllAsync();
        if (response.Status)
        {
            sbutotalDataDtos =response.Result.Select(x => new ChartDoubleDataDto()
                {
                    Label = x.YearMonth.ToString("yyyy-MM"),
                    Value = x.FsSubtotal
                }).ToList();
            recvTrendDataDtos = response.Result.Select(x => new ChartDoubleDataDto
                {
                    Label = x.YearMonth.ToString("yyyy-MM"),
                    Value = x.RecvValue
                }).ToList();
            consTrendDataDtos = response.Result.Select(x => new ChartDoubleDataDto
                {
                    Label = x.YearMonth.ToString("yyyy-MM"),
                    Value = x.ConsValue
                }).ToList();
        }

        var ds = new ChartDataSource();
        ds.Options.X.Title = "Month";
        ds.Options.Y.Title = "Inventory";
        ds.Options.X.Stacked = false;
        ds.Options.Y.Stacked = false;
        ds.Labels = sbutotalDataDtos.Select(x => x.Label);
        ds.Data.Add(new ChartDataset
            {
                BorderWidth = 2,
                Label = "Inventory",
                Data = sbutotalDataDtos.Select(x => x.Value).Cast<object>(),
                PointStyle = ChartPointStyle.Circle,
                PointRadius = 3,
                PointHoverRadius = 6
            });
        ds.Data.Add(new ChartDataset
            {
                BorderWidth = 1,
                Label = "Received",
                Data = recvTrendDataDtos.Select(x => x.Value).Cast<object>(),
                PointStyle = ChartPointStyle.CrossRot,
                PointRadius = 3,
                PointHoverRadius = 6
            });
        ds.Data.Add(new ChartDataset
            {
                BorderWidth = 2,
                Label = "Consumption",
                Data = consTrendDataDtos.Select(x => x.Value).Cast<object>(),
                PointStyle = ChartPointStyle.RectRot,
                PointRadius = 3,
                PointHoverRadius = 6
            });
        return ds;
    }
}
